﻿using System;
using System.Collections.Generic;
using System.Data;
using WebProject.Core.Models;
using WebProject.Core.Repositories;

namespace WebProject.Core.Service
{
    public class ProductionService : IProductionService
    {
        #region[Private Attribute]
        private readonly IProductionRepository m_ProductionRepository;
        private readonly IProductionTypeRepository m_ProductionTypeRepository;
        #endregion

        #region[Constructor]
        public ProductionService(IProductionRepository productionRepository, IProductionTypeRepository productionTypeRepository)
        {
            if (productionRepository == null)
            {
                throw new ArgumentNullException("productionRepository");
            }
            if (productionTypeRepository == null)
            {
                throw new ArgumentNullException("productionTypeRepository");
            }
            m_ProductionRepository = productionRepository;
            m_ProductionTypeRepository = productionTypeRepository;
        }
        #endregion

        #region[Implement Methods]
        public int Add(Production production)
        {
            if (production == null)
            {
                throw new ArgumentNullException("production");
            }
            return m_ProductionRepository.Add(production);
        }

        public bool Remove(Production production)
        {
            if (production == null)
            {
                throw new ArgumentNullException("production");
            }
            return m_ProductionRepository.Remove(production) != 0;
        }

        public bool Remove(int id)
        {
            var target = m_ProductionRepository.GetById(id);
            if (target == null)
            {
                throw new ObjectNotFoundException(string.Format("Target Production with Id={0} not found", id));
            }
            return m_ProductionRepository.Remove(target) != 0;
        }

        public bool Update(Production production)
        {
            if (production == null)
            {
                throw new ArgumentNullException("production");
            }
            return m_ProductionRepository.Update(production) != 0;
        }

        public IList<Production> GetAllProductions(bool isOnlyDisplayOnIndex)
        {
            return m_ProductionRepository.GetAll(isOnlyDisplayOnIndex);
        }

        public Production GetProductionById(int id)
        {
            return m_ProductionRepository.GetById(id);
        }

        public IList<Production> GetProductionsByTypeId(int productionTypeId)
        {
            return m_ProductionRepository.GetProductionByTypeId(productionTypeId);
        }

        public IList<Production> GetProductionsByType(ProductionType productionType)
        {
            if (productionType == null)
            {
                throw new ArgumentNullException("productionType");
            }
            return m_ProductionRepository.GetProductionByTypeId(productionType.Id);
        }
        #endregion
    }
}